/*
 * @Author: 追逐的娃 kangchao_php@163.com
 * @Date: 2024-07-11 15:36:55
 * @LastEditors: 追逐的娃 kangchao_php@163.com
 * @LastEditTime: 2024-10-25 17:57:47
 * @FilePath: \vue-project\src\utils\util.js
 * @Description: 
 * 
 */
/**
 * 返回价格乘以数量，经度丢失函数
 * @param {*} arg1 数量
 * @param {*} arg2 价格
 */
export const NumberMul = (arg1, arg2) => {
    var m = 0
    var s1 = arg1.toString()
    var s2 = arg2.toString()
    try {
        m += s1.split('.')[1].length
    } catch (e) { }
    try {
        m += s2.split('.')[1].length
    } catch (e) { }

    return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}

/**
 * 下载文件并更新文件名称
 * @param {*} fileUrl 要下载的文件路径
 * @param {*} fileName 要下载的文件新名称后面需要加后缀不然就会下载成htm文件
 */
export const DownloadAndUpdateFileName = (fileUrl, fileName) => {
    const x = new window.XMLHttpRequest();
    x.open('GET', fileUrl, true);
    x.responseType = 'blob';
    x.onload = () => {
        const url = window.URL.createObjectURL(x.response);
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        a.click();
    };
    x.send();
}

/**
 * 获取文件类型
 * @param {*} fileName 
 */
export const getFileType = (fileName) => {
    if (!fileName) return 'other';
    //根据文件名提取后缀名
    const index = fileName.lastIndexOf('.');
    const ext = fileName.substr(index + 1).toLowerCase();
    const enumsFileType = {
        image: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg', 'tiff'],
        pdf: ['pdf'],
        excel: ['xlsx'],
        word: ['docx', 'doc'],
        ppt: ['ppt', 'pptx'],
        video: ['mp4', 'avi', 'mov']
    };
    for (const key in enumsFileType) {
        if (enumsFileType[key].includes(ext)) {
            return key;
        }
    }
    return 'other';
};